/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlers;

import BankAuth.CreditCardAuthority;
import BankAuth.ExternalBank;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import model.Store;

/**
 *
 * @author Tasos
 * O Payment Controler einai ena voithitikos Controler
 * o opoios einai sthn ousia enas eksidikeymenos DB fetcher
 * pou afora ergasies pou prepei na ginoun gia thn plhrwmh
 * O Controler aytos se synergasia me ton Controler pou
 * yparxei sthn idia thn forma synthetoun to Control Level
 * Gia thn Payment From
 */
public class PaymentControler {
private static EntityManager emBank;
boolean valid;
    
public PaymentControler() {
    //Arxikopoihsh toy entity Manager
    EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("BankPU");
    emBank = emf2.createEntityManager();
    this.valid = false;
    }

//public ArrayList<ExternalBank> banklist = new ArrayList<>(); //Debug

//public method pou epistrefei mia lista me tis trapezes
public List<ExternalBank> getBanks()
{
TypedQuery<ExternalBank> allBanksQuery = emBank.createNamedQuery("ExternalBank.findAll", ExternalBank.class);
List<ExternalBank> bankResults = allBanksQuery.getResultList();
return bankResults;    
}

////public method pou epistrefei mia lista me ta stoixeia toy pelath apo ton arithmo pistwtikhs kartas
public ArrayList<String> getCredentials(String CardId)
{
//Dhmiourgia Query kai fetch apotelesmatwn
TypedQuery<CreditCardAuthority> credentialsQuery = emBank.createNamedQuery("CreditCardAuthority.findByNumber", CreditCardAuthority.class);
credentialsQuery.setParameter("number", CardId);
List<CreditCardAuthority>  credentialsList = credentialsQuery.getResultList();
ArrayList<String> credentials = new ArrayList();
//An vrethike Eggrafh
if (credentialsList.size()>0){
//Dhmiourgia enos prosorinou natikeimenou me ta stoixei pou xreiazomaste
CreditCardAuthority tempCredentialObj = credentialsList.get(0);
//kai prosthiki twn stoixeiwn sthn ArrayList
credentials.add(tempCredentialObj.getOwnerName());
credentials.add(tempCredentialObj.getCvv());
credentials.add(tempCredentialObj.getBank().getBankId().toString());
}
return credentials;
}



}



